package com.itheima.service.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

// 业务层的实现类
public class BrandServiceImpl implements BrandService {
    // SqlSessionFactory可以看做数据库连接池,一个项目只要一个数据库连接池
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Brand> selectAll() {
        // 使用DAO层查询所有数据
        // sqlSession相当于数据连接,每次操作都需要拿到一个新的SqlSession,用完就需要关掉
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brands = mapper.selectAll();

        sqlSession.close();

        return brands;
    }

    @Override
    public void add(Brand brand) {
        // 调用DAO的添加品牌
        // SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.add(brand);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteByIds(int[] ids) {
        // 调用DAO的批量删除
        // SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.deleteByIds(ids);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public PageBean selectByPage(int currentPage, int pageSize) {
        // int currentPage: 当前页码
        // int pageSize: 每页的数量
        // 调用DAO分页查询数据
        // SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        // 计算跳过的数量:  跳过的数量 = (当前页-1)*每页的数量
        int begin = (currentPage - 1) * pageSize;
        // 获取当前页数据
        List<Brand> brands = mapper.selectByPage(begin, pageSize);
        // 获取总数量
        int totalCount = mapper.selectTotalCount();

        PageBean pageBean = new PageBean();
        pageBean.setTotalCount(totalCount);
        pageBean.setRows(brands);

        sqlSession.close();
        return pageBean;
    }

    @Override
    public PageBean selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        // int currentPage: 当前页码
        // int pageSize: 每页的数量
        // 调用DAO分页查询数据
        // SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        // 处理模糊查询问题 米 -> %米%
        String brandName = brand.getBrandName();
        if (brandName != null && brandName.length() > 0) {
            String newBrandName = "%" + brandName + "%";
            brand.setBrandName(newBrandName);
        }

        String companyName = brand.getCompanyName();
        if (companyName != null && companyName.length() > 0) {
            String newCompanyName = "%" + companyName + "%";
            brand.setCompanyName(newCompanyName);
        }

        // 计算跳过的数量:  跳过的数量 = (当前页-1)*每页的数量
        int begin = (currentPage - 1) * pageSize;
        // 获取当前页数据
        List<Brand> brands = mapper.selectByPageAndCondition(begin, pageSize, brand);
        // 获取总数量
        int totalCount = mapper.selectTotalCountByCondition(brand);

        PageBean pageBean = new PageBean();
        pageBean.setTotalCount(totalCount);
        pageBean.setRows(brands);

        sqlSession.close();
        return pageBean;
    }
}
